
var util=require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    show:{},     
    month:'2019-11'   
  }, 
  onLoad:function(){  
    this.monthStatistic(); 
  },
  bindDetail:function(id){  
    
    var show=this.data.show;
    show["detail" + id] = show["detail" + id]?false:true
    this.setData({
      show: show 
    })   
              
  },     
  monthStatistic:function(){
    var that=this; 
    var month = that.data.month;
    var url = util.url.shop_order_monstatis;
    url += "?month=" + month;
    util.fetchGet(
      url,
      function(ret){
        var data=ret.data; 
        for(var i=0;i<data.length;i++){
          data[i].time = data[i].year+"-"+(data[i].month+1)+"-"+data[i].day
        }      
        that.setData({   
          list:data
        })   
      }    
    )  
  } ,
  dayStatistic:function(e){
    var date = e.currentTarget.dataset.id;
    var that = this;
    var data = that.data.list;
    var index=-1;
    for (var i = 0; i < data.length; i++) {
      if (data[i].time == date) {
        if (!data[i].foods){
          index=i;
        }
      }
    }
    if (index==-1){
      that.bindDetail(date);
      return;
    }
    
      var url = util.url.shop_order_daystatis;
    url += "?date=" + date;
    util.fetchGet(
      url,
      function (ret) {      
        data[index].foods =ret.data;  
        that.setData({
          list: data 
        })
        that.bindDetail(date); 
      }
    )  
  
  },
  bindDateChange:function(e){
    this.setData({
      month: e.detail.value
    })
    this.monthStatistic();
  }        
})